<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Rooms extends CI_Controller {
    
    public function index()
    {
        $this->load->model("Mlobby");
        $this->Mlobby->deleteExpired();
        if(!$this->Mlobby->checkVisitorIsPlayer($this->session->userdata('session_id'))){
            redirect('/lobby', 'location', 302); return;
        } else {
            /*$usersonline = $this->Mlobby->getUsersOnline();
            if($usersonline != null)
            {
                foreach($usersonline as $u)
                {
                    if($u["isPlaying"] == 1)
                    {
                        $multi[] = $u;
                    } else {
                        $single[] = $u;
                    }
                }
                if(isset($multi)){
                        foreach ($multi as $m) {
                            $mult[] = $m["room"];
                        }
                        asort($mult); $last = -5; 
                        foreach($mult as $key => $val)
                        {
                            $multi_ordre[] = array($multi[$key], $last);
                            $last = $multi[$key]["room"];
                        }
                        $data["multi"] = $multi_ordre;
                } else {  $data["multi"] = array();  }
                if(!isset($single)){$single = array(); }
                $data["single"] = $single;
                $data["users"] = $usersonline;
            }*/
            $data["me"] = $this->Mlobby->getUserInfo($this->session->userdata("session_id"));
            $this->load->view("rooms/index.php", $data);
        }
    }
    
    public function reloadRooms(){
        $this->load->model("Mlobby");
        $this->Mlobby->deleteExpired();
        $usersonline = $this->Mlobby->getUsersOnline();
        if($usersonline != null)
        {
            foreach($usersonline as $u)
            {
                if($u["isPlaying"] == 1)
                {
                    $multi[] = $u;
                } else {
                    $single[] = $u;
                }
            }
            if(isset($multi)){
                foreach ($multi as $m) {
                    $mult[] = $m["room"];
                }
                asort($mult); $last = -5; 
                foreach($mult as $key => $val)
                {
                    $multi_ordre[] = array($multi[$key], $last);
                    $last = $multi[$key]["room"];
                }
            } else { $multi_ordre = array(); }
            if(!isset($single))
            {
                $single = array();
            }
            $data["multi"] = $multi_ordre;
            $data["single"] = $single;
            $data["users"] = $usersonline;
            $data["status"] = true;
        } else {
            $data["status"] = false;
        }
        $this->load->view("lobby/xml_users.php", $data); 
    }
    
    public function changePerso($perso)
    {
		$this->load->model("Mlobby");
        $this->Mlobby->deleteExpired();
        if(!$this->Mlobby->checkVisitorIsPlayer($this->session->userdata('session_id'))){
            return;
        } else {
			if(!is_nan($perso))
			{
				$this->load->model("Mlobby");
				$this->Mlobby->changePerso($this->session->userdata("session_id") , $perso);
			} 
		}
    }
    
    public function contact()
    {
		$this->load->model("Mlobby");
        $this->Mlobby->deleteExpired();
        if(!$this->Mlobby->checkVisitorIsPlayer($this->session->userdata('session_id'))){
            redirect('/lobby', 'location', 302); return;
        } else {
			$data["me"] = $this->Mlobby->getUserInfo($this->session->userdata("session_id"));
			$this->load->view("rooms/contacts", $data);
		}
    }
    
    public function rules()
    {
		$this->load->model("Mlobby");
        $this->Mlobby->deleteExpired();
        if(!$this->Mlobby->checkVisitorIsPlayer($this->session->userdata('session_id'))){
            redirect('/lobby', 'location', 302); return;
        } else {
			$data["me"] = $this->Mlobby->getUserInfo($this->session->userdata("session_id"));
			$this->load->view("rooms/rules", $data);
		}
    }
    
    public function ranking()
    {
		$this->load->model("Mlobby");
        $this->Mlobby->deleteExpired();
        if(!$this->Mlobby->checkVisitorIsPlayer($this->session->userdata('session_id'))){
            redirect('/lobby', 'location', 302); return;
        } else {
			$data["me"] = $this->Mlobby->getUserInfo($this->session->userdata("session_id"));
			$this->load->view("rooms/ranking", $data);
		}
    }
    
}